package utils;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class EmailUtils {

    public static void emailSend(String msg, String email) throws MessagingException {
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");// 连接协议
        properties.put("mail.smtp.host", "smtp.163.com");// 主机名
        properties.put("mail.smtp.port", 465);// 端口号
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
        // 创建验证器
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("qq2477182366@163.com", "OGKUXVLZSWTIDAIE");
            }
        };
        Session session = Session.getInstance(properties, auth);
        // 2.创建一个Message，它相当于是邮件内容
        Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("qq2477182366@163.com")); // 设置发送者
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者
            message.setSubject("用户激活");
            // message.setText("这是一封激活邮件，请<a href='#'>点击</a>");
            message.setContent(msg, "text/html;charset=utf-8");
            // 3.创建 Transport用于将邮件发送
            Transport.send(message);
    }
}
